using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using TPL.Xna.Common;

namespace GoldDigger.Sprites
{
    internal class BirdSprite : SpriteSheetItemSprite
    {
        private readonly float _speed;

        /// <summary>
        /// Animate the flying across screen
        /// </summary>
        private class FlyingAnimator : SpritePropertyAnimator<Vector2>
        {
            public float Step;
            public float Min;
            public float Max;

            private bool _minToMax = true;
            private Vector2 _step;

            public event EventHandler<EventArgs> DirectionChanged;

            private void InvokeDirectionChanged()
            {
                EventHandler<EventArgs> handler = DirectionChanged;
                if (handler != null) handler(this, null);
            }

            public FlyingAnimator()
                : base(s => s.Position)
            {
            }

            public override void Initialize()
            {
                _step = new Vector2(Step, 0);
            }

            protected override void Update(ref Vector2 value, Vector2 delta)
            {
                value += delta;
            }

            protected override Vector2 CalculateDelta()
            {
                var x = GetValue().X;
                if (_minToMax)
                {
                    x += Step;
                    if (x > Max)
                    {
                        _minToMax = false;
                        InvokeDirectionChanged();
                    }
                    return _step;
                }
                else
                {
                    x -= Step;
                    if (x < Min)
                    {
                        _minToMax = true;
                        InvokeDirectionChanged();
                    }
                    return -_step;
                }
            }
        }

        /// <summary>
        /// Create with speed.
        /// </summary>
        /// <param name="spriteSheetItemInfo"></param>
        /// <param name="speed"></param>
        public BirdSprite(SpriteSheetItemInfo spriteSheetItemInfo, float speed)
            : base(spriteSheetItemInfo)
        {
            _speed = speed;
        }

        /// <summary>
        /// current animator.
        /// </summary>
        private FlyingAnimator _flyingAnimator;

        /// <summary>
        /// Load animator
        /// </summary>
        public override void LoadContent()
        {
            AddAnimator(_flyingAnimator = new FlyingAnimator
                                              {
                                                  Min = 0,
                                                  Max = 800,
                                                  Step = _speed,
                                              });

            Effect = SpriteEffects.FlipHorizontally;
            _flyingAnimator.DirectionChanged +=
                (s, e) =>
                    {
                        Effect = (Effect == SpriteEffects.None)
                                     ? SpriteEffects.FlipHorizontally
                                     : SpriteEffects.None;
                    };
            
            // Attach animator to the shape also to make it fly together with the bird
            _flyingAnimator.AttachTo(SpriteArea.Shape);
            base.LoadContent();
        }

        public override void MarkAsBeingCollected()
        {
            _flyingAnimator.Enable = false;
        }
    }
}
